﻿package  {
	
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import Bullet;
	import flash.net.drm.VoucherAccessInfo;
	
	public class Raticate extends MovieClip {
		public static const HAUT:Number = 0;
		public static const DROITE:Number = 1;
		public static const BAS:Number = 2;
		public static const GAUCHE:Number = 3;
		public static const MOVEMENT_SPEED:Number = 10;
		public static const INIT_X:Number = 350;
		public static const INIT_Y:Number = 200;
		public static const STAGE_HEIGHT = 650;
		
		var playerName:TextField;
		var m_Id:Number;
		
		public function Raticate()
		{
			//Put the player name above his head
			playerName = new TextField();
			playerName.selectable = false;
			playerName.text = "Unknown";
			playerName.autoSize = flash.text.TextFieldAutoSize.LEFT;
			playerName.x+= 6;
			this.addChild(playerName);
			
			//Define a position
			this.x = Raticate.INIT_X;
			this.y = Raticate.INIT_Y;
		}
		
		public function setName(p_Name:String):void
		{
			this.playerName.text = p_Name;
		}
		
		public function getName():String
		{
			return this.playerName.text;
		}
		
		public function setPosition(p_X:Number,p_Y:Number):void
		{
			this.x = p_X;
			this.y = p_Y;
		}
		
		public function getX():Number
		{
			return this.x;
		}
		
		public function getY():Number
		{
			return this.y;
		}
		
		public function setId(p_Id:Number):void
		{
			this.m_Id = p_Id;
		}
		
		public function getId():Number
		{
			return this.m_Id;
		}
		
		public function movePlayer(dir:Number):void
		{
			switch (dir)
			{
				case Raticate.HAUT :
				{
					if (this.y - Raticate.MOVEMENT_SPEED >= 0)
						this.y -= Raticate.MOVEMENT_SPEED;
					break;
				}
				case Raticate.DROITE :
				{
					if(this.x + this.width + Raticate.MOVEMENT_SPEED <= stage.width)
						this.x += Raticate.MOVEMENT_SPEED;
					break;
				}
				case Raticate.BAS :
				{
					if (this.y + Raticate.MOVEMENT_SPEED <= stage.height)
						this.y+= Raticate.MOVEMENT_SPEED;
					break;
				}
				case Raticate.GAUCHE :
				{
					if(this.x - Raticate.MOVEMENT_SPEED >= 0)
						this.x -= Raticate.MOVEMENT_SPEED;
					break;
				}
			}
		}
		
		public function fire(p_Direction:Number):Bullet
		{
			var b:Bullet = new Bullet(this.x+(this.width/2),this.y+this.height-4);
			stage.addEventListener(Event.ENTER_FRAME, function fireHandler(e:Event){
				b.y += 5;
				if (b.y >= STAGE_HEIGHT)
				{
					stage.removeEventListener(Event.ENTER_FRAME,fireHandler);
				}
			});
			
			return b;
		}
	}
	
}
